package com.suneast.suneaststore.modules.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.homepage.HomePageFragment;
import com.suneast.suneaststore.modules.main.event.GotCartCount;
import com.suneast.suneaststore.modules.main.event.SignOutDateEvent;
import com.suneast.suneaststore.modules.personal.PersonalFragment;
import com.suneast.suneaststore.modules.personal.sign.SignInFragment;
import com.suneast.suneaststore.modules.shopcart.ShopCartFragment;
import com.suneast.suneaststore.modules.shopcart.ShopCartHandler;
import com.suneast.suneaststore.modules.sort.SortFragment;
import com.suneast.suneaststore.ui.fragments.bottom.AbsBottomFragment;
import com.suneast.suneaststore.ui.fragments.bottom.BottomItemFragment;
import com.suneast.suneaststore.ui.fragments.bottom.BottomTabBean;
import com.suneast.suneaststore.ui.fragments.bottom.ItemBuilder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedHashMap;

public class BottomFragment extends AbsBottomFragment {

    @Override
    public LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(ItemBuilder builder) {
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = new LinkedHashMap<>();
        items.put(new BottomTabBean(getString(R.string.home_page),
                R.drawable.ic_home_normal, R.drawable.ic_home_selected), new HomePageFragment());
        items.put(new BottomTabBean(getString(R.string.category),
                R.drawable.ic_sort_normal, R.drawable.ic_sort_selected), new SortFragment());
        items.put(new BottomTabBean(getString(R.string.shopping_cart),
                R.drawable.ic_cart_normal, R.drawable.ic_cart_selected), new ShopCartFragment());
//        items.put(new BottomTabBean(getString(R.string.news),
//                R.drawable.ic_msg_normal, R.drawable.ic_msg_selected), new MsgListFragment());
        items.put(new BottomTabBean(getString(R.string.mine),
                R.drawable.ic_personal_normal, R.drawable.ic_personal_selected), new PersonalFragment());
        return builder.addItems(items).build();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        super.onBindView(savedInstanceState, rootView);
        EventBus.getDefault().register(this);
        // 请求购物车商品数量 请求消息数量
        if (AccountManager.getSignState()) {
            ShopCartHandler.requestCartCount();
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotCartCount(GotCartCount event) {
        if (event.getResult() == Flag.Result.SUCCEED) {
            setMessageNumber(2, event.getGoodsCount());
        } else {
            setHasMessage(2, false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSignOutDate(SignOutDateEvent event) {
        AccountManager.exitCurrentAccount();
        start(new SignInFragment().withState(1));
    }
}
